package org.jspar.route;

import java.util.List;

import org.jspar.model.Net;
import org.jspar.tile.Tile;

public class ComplexCongestionRule implements ICongestionRule {
	public int set(Arc a, GlobalRouting routing) {
		int crossoverCount = 0, cornerCount = 0;

		/* Count the corners in this tile: */
		for (Net n : a.nets())
			cornerCount++;

		Tile[] routingRoot;
		/* Examine each tile that crosses this tile... */
		List<Tile> temp = collectTiles(a.tile(), routing
				.getRootTile(GlobalRouting.toggleDirection(a.page())));
		for (Tile t : temp) {
			/* Count the number of nets that are in the perpendicular tile: */
			Arc perpA = (Arc) t.content();
			for (Net n : perpA.nets()) {
				crossoverCount++;
			}
		}
		a.setCongestion(cornerCount * crossoverCount + cornerCount);
		return a.congestion();
	}

	private List<Tile> collectTiles(Tile start, Tile root) {
		return root.areaEnumerate(new FreeToRouteIn(), start.y(), start.x(),
				start.height(), start.width());
	}

}
